package com.example.administrator.x2picturetransliteration.Home.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.x2picturetransliteration.R;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.ArrayList;
import java.util.List;

/**
 * 导出
 * Created by Administrator on 2019/8/21.
 */

public class ExportDialog extends Dialog{

    private TextView formatText;
    private View formatView;

    private ListView listview;

    private String format = "txt";
    private List<String> data = new ArrayList<>();

    private Context context;

    private hd mhd;

    private Boolean judge = false; //是否表格识别

    public ExportDialog(@NonNull Context context,hd mhd) {
        super(context, R.style.HomeDialog);

        this.context = context;

        this.mhd = mhd;
    }

    public ExportDialog(@NonNull Context context,Boolean judge,hd mhd) {
        super(context, R.style.HomeDialog);

        this.context = context;

        this.judge = judge;

        this.mhd = mhd;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_dialog_export);

        formatText = findViewById(R.id.formatText);
        formatView = findViewById(R.id.formatView);

        if (judge==null){

            data.add("Text");
            data.add("Word");
            data.add("Excel");
            data.add("PDF");

        }else if (judge){

            data.add("Excel");
            formatText.setText("Excel");
            data.add("Text");
            data.add("Word");
            data.add("PDF");

        }else {

            data.add("Word");
            formatText.setText("Word");
            data.add("Text");
            data.add("Excel");
            data.add("PDF");

        }


        listview = findViewById(R.id.listview);

        listview.setAdapter(new ExportAdapter());

        formatView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                listview.setVisibility(listview.getVisibility()==View.GONE?View.VISIBLE:View.GONE);

            }
        });

        findViewById(R.id.endView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ExportDialog.this.dismiss();
            }
        });

        /** QQ */
        findViewById(R.id.qqView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mhd.hdff(SHARE_MEDIA.QQ,type());

            }
        });

        /** 微信 */
        findViewById(R.id.wxView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mhd.hdff(SHARE_MEDIA.WEIXIN,type());
            }
        });

        /** 生成二维码 */
        findViewById(R.id.qrView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mhd.hdff2(true,type());
            }
        });

        /** 生成链接 */
        findViewById(R.id.linkView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mhd.hdff2(false,type());
            }
        });

    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity= Gravity.BOTTOM;
        layoutParams.width= WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height= WindowManager.LayoutParams.WRAP_CONTENT;

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);

    }

    private class ExportAdapter extends BaseAdapter{


        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            TextView textView = new TextView(context);

            textView.setText(data.get(position));

            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    formatText.setText(data.get(position));
                    listview.setVisibility(View.GONE);
                }
            });

            return textView;
        }
    }


    public interface hd{

        void hdff(SHARE_MEDIA type, String word);

        void hdff2(boolean qr,String word);
    }

    private String type(){

        if ("Text".equals(formatText.getText() + "")){

            return "1";

        }else if ("Word".equals(formatText.getText() + "")){

            return "2";

        }else if ("PDF".equals(formatText.getText() + "")){

            return "3";

        }else {

            return "4";
        }

    }

}
